const express=require("express")
const pool=require("../pool")
const server=express.Router();


// 加入我的收藏
server.get("/toshoucang",(req,res)=>{
  var pid=parseInt(req.query.pid);
  var timu=req.query.timu;
  var answer=req.query.answer;
  var fenlei=req.query.fenlei;
  //console.log(pid,timu,answer,fenlei)
  var sql="SELECT id FROM shoucang WHERE pid=? AND fenlei=?"
  pool.query(sql,[pid,fenlei],(err,result)=>{
    if(err) throw err;
    if(result.length==0){
      var sql=`INSERT INTO shoucang VALUES(null,${pid},"${timu}","${answer}","${fenlei}")`
    }else{
      return;
    }
    pool.query(sql,(err,result)=>{
      if(err) throw err;
      res.send({code:1,data:result})
      console.log(result)
    })
  })
})

// 我的收藏
server.get("/shoucang",(req,res)=>{
  var sql="SELECT*FROM shoucang";
  pool.query(sql,(err,result)=>{
    if(err) throw err;
    res.send({code:1,data:result})
  })
})

// 删除选中的收藏
server.get("/delShoucang",(req,res)=>{
  var id=req.query.id;
  var sql="DELETE FROM shoucang WHERE id=?";
  pool.query(sql,[id],(err,result)=>{
    if(err) throw err;
    if(result.affectedRows>0){
      res.send({code:1,msg:"删除成功"})
    }else{
        res.send({code:-1,msg:"删除失败"})
    }
  })
})







module.exports=server;


